package com.xw.bo.ks.controller.no;

import com.xw.bo.ks.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.filefilter.AbstractFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author： XW -> ~
 * @create： 2024/6/12 17:00
 */
@Slf4j
@RestController
@RequestMapping("/log")
@Api(tags = "文件日志 API", description = "文件日志 API")
public class LogFileInfoController {

    @Value("${log.address:}")
    private String logAddress;

    @GetMapping(value = "/fileList")
    @ApiOperation(value = "文件列表", notes = "文件列表")
    public List<LogInfoList> fileList(@RequestParam(name = "dic", required = false) String dic) {

        File directory = null;
        if (StringUtils.isNotEmpty(dic)) {
            directory = new File(logAddress + dic);
        } else {
            directory = new File(logAddress);
        }


        try {
//            List<File> files = FileUtil.listFiles(directory, false, fileFilter, dirFilter);
            List<LogInfoList> infoLists = new ArrayList<>();

            File[] files = directory.listFiles();
            for (File file : files) {

                LogInfoList logFile = new LogInfoList();
                logFile.setName(file.getName());
                logFile.setDate(file.getName());
                logFile.setAddress(file.getAbsolutePath());
                logFile.setDirectory(file.getPath().replace(logAddress,""));
                logFile.setSize(file.length() + "");
                logFile.setPath(directory.getAbsolutePath());
                logFile.setIsFile(file.isFile());
                infoLists.add(logFile);
            }
//            for (File file : files) {
//                LogInfoList logFile = new LogInfoList();
//                logFile.setName(file.getName());
//                logFile.setDate(file.getName());
//                logFile.setAddress(file.getAbsolutePath());
//                logFile.setDirectory(directory.getAbsolutePath());
//                logFile.setSize(file.length() + "");
//                logFile.setPath(directory.getAbsolutePath());
//                logFile.setIsFile(file.isFile());
//                infoLists.add(logFile);
//            }
            return infoLists;
        } catch (Exception e) {
            log.error("文件遍历错误", e);
        }
        return new ArrayList<>();
    }

    @GetMapping("/downloadFile")
    @ResponseBody
    public void download(HttpServletResponse response, @RequestParam String fileName) {
        String result = FileUtil.downloadFile(response, logAddress, fileName);
        System.out.println(result);
    }

    @Data
    public static class LogInfoList {
        String date;
        String name;
        String size;
        String address;
        String path;
        String directory;
        Boolean isFile;
    }


    IOFileFilter fileFilter = new AbstractFileFilter() {
        @Override
        public boolean accept(File file) {
            return true;
        }
    };

    IOFileFilter dirFilter = new AbstractFileFilter() {
        @Override
        public boolean accept(File file) {
            return false;
        }
    };


}
